<?php

namespace app\admin\controller\report;

use app\admin\model\exchange\Order;
use app\admin\model\station\Station;
use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;
use think\Session;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Orderstore extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\report\Order
     */
    protected $model = null;
    protected $agent_id = -1;
    protected $store_no = -1;
    protected $admin_id = -1;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\report\Order;
        $user_info = Session::get('admin');//获取当前登录用户信息
        $this->agent_id = $user_info['agent_id'];
        $this->store_no = $user_info['store_no'];
        $this->admin_id = $user_info['id'];
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->where('store_no',$this->store_no)//油站绑定代理商
            ->order($sort, $order)
            ->paginate($limit);
        //获取全部油站信息
//        $all_station = [];
//        $all_station_tmp = Station::all();
//        foreach ($all_station_tmp as $vvv){
//            $all_station[$vvv->store_no] = $vvv->name;
//        }
        $re_list = [];
        foreach ($list->items() as $item){
//            $item['station_name'] = isset($all_station[$item['store_no']]) ? $all_station[$item['store_no']] : '';
            $re_list[] = $item;
        }
        $result = ['total' => $list->total(), 'rows' => $re_list];
        return json($result);//
    }

    /**
     * 删除
     *
     * @param $ids
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function del($ids = null)
    {
        $this->error(__('删你鸡巴~'));
    }


    public function dsorder()
    {
        //根据店铺获取前一天订单数据
        $yestodaytime = strtotime('-1 day');
        $yestodaydate = date('Y-m-d',$yestodaytime);
        $now_date  = date('Y-m-d 00:00:00');
        $now_date_time = strtotime($now_date);


//        $yestodaytime = strtotime('2023-1-15 00:00:00'); //9号数据要删除
//        $yestodaydate = date('Y-m-d',$yestodaytime);//9号数据要删除
//        $now_date  = date('2023-1-16 00:00:00');
//        $now_date_time = strtotime($now_date);


        $all_order_tmp = Order::where('createtime','>=',$yestodaytime)->where('createtime','<',$now_date_time)->select();
//        var_dump($all_order_tmp);die;
        if($all_order_tmp){
            $all_order = [];
            $all_save_info = [];
            foreach ($all_order_tmp as  $item){
                if(isset($item->store_no) && $item->store_no){
                    $tmp_store_no = $item->store_no.'##@##'.$item->agent_id;
                    $all_order[$tmp_store_no]['pay_price'][] = $item->total_pay_price;
                    $all_order[$tmp_store_no]['pay_num'][] = 1;
                    if($item->is_pay == 1){
                        //退款
                        $all_order[$tmp_store_no]['refund_price'][] = $item->total_pay_price;
                        $all_order[$tmp_store_no]['refund_num'][] = 1;
                    }

                    if($item->is_pay == 2){
                        //已付款
                        $all_order[$tmp_store_no]['real_price'][] = $item->total_pay_price;
                        $all_order[$tmp_store_no]['real_num'][] = 1;
                    }

                    unset($tmp_store_no);
                }
            }
//            var_dump($all_order);die;
            if ($all_order){
                foreach ($all_order as $key=>$val){
                    $tmp['store_no'] = 0;
                    $tmp['agent_id'] = 0;
                    $store_no_agent_id_arr = explode('##@##',$key);
                    if(isset($store_no_agent_id_arr[0]) && $store_no_agent_id_arr[0]){
                        $tmp['store_no'] = $store_no_agent_id_arr[0];
                    }
                    if(isset($store_no_agent_id_arr[1]) && $store_no_agent_id_arr[1]){
                        $tmp['agent_id'] = $store_no_agent_id_arr[1];
                    }
                    if(!$tmp['store_no'] || !$tmp['agent_id']){
                        continue;
                    }
                    $tmp['pay_price'] = isset($val['pay_price']) && $val['pay_price'] ? array_sum($val['pay_price']):0;
                    $tmp['pay_num'] = isset($val['pay_num']) && $val['pay_num'] ? count($val['pay_num']):0;
                    $tmp['refund_price'] = isset($val['refund_price']) && $val['refund_price'] ? array_sum($val['refund_price']):0;
                    $tmp['refund_num'] = isset($val['refund_num']) && $val['refund_num'] ? count($val['refund_num']):0;
                    $tmp['real_price'] = isset($val['real_price']) && $val['real_price'] ? array_sum($val['real_price']):0;
                    $tmp['real_num'] = isset($val['real_num']) && $val['real_num'] ? count($val['real_num']):0;
                    $tmp['date'] = $yestodaydate;

                    $all_save_info[] = $tmp;

                    unset($tmp);
                }
            }

            $order_report_model = new \app\admin\model\report\Order();
            //删除原来数据
            $order_report_model->where('date',$yestodaydate)->delete();

            //新增数据
            if($all_save_info){
                $order_report_model->saveAll($all_save_info);
            }

        }

    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
